<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Adm_User extends Controller_Base_Backend {

	public function action_index() {
		$this->view->body->content = Smarty_View::factory("auth/adm/user.tpl");
		$user = new Model_User($this->request->param('id'));
		$this->view->body->content->user = $user;
		$this->view->body->content->system_roles = Model_Role::get_all_as_array();
		$this->view->body->content->selected_roles = array_keys($user->get_roles_as_array());
		$this->response->body($this->view);
	}

	public function action_list() {
		$this->view->body->content = Smarty_View::factory("auth/adm/list_user.tpl");
		$report = new Report(Model_User::get_report());
		if (Model_User::logged_in("admin")) {
			$report->bind("id", __("Id"));
		} else {
			$report->hide("id");
		}
		$report->bind("name", __("Name"), array("Report_DEC", "format_name"));
		$report->bind("email", __("Email"));
		$report->bind("password", __("Password"));
		$report->bind("logins", __("Logins"));
		$report->bind("last_login", __("Last Login"));
		$report->bind("actions", __("Actions"), array("Controller_Adm_User", "format_actions"));
		$report->add_filter("name", __("Name"), "text");
		$report->add_filter("email", __("Email"), "text");
		$report->add_filter("last_login",__("Last Login"),"date");
		$this->view->body->content->report = $report->parse();
		$this->response->body($this->view);
	}
	
	public function action_remove() {
		try {
			$user = Model_User::factory($this->request->param('id'));
			$user->remove();
			Request::current()->redirect("adm/user/list/");
		} catch (Exception $e) {
			$errors = array("_external" => array("user" => $e->getMessage()));
			$this->response->body(Json_View::factory(array("content" => $errors)));
		}
	}

	public function action_save() {
		try {
			$user = Model_User::factory($_POST['user_id']);
			if(($user->get_id() && $user->get_email()!=$_POST['email']) || $user->get_id()==0) {
				Model_User::unique_email($_POST['email']);
			}
			$user->set_email($_POST['email']);
			$user->set_name($_POST['name']);
			if(isset($_POST['password']) && !empty($_POST['password'])) {
				$user->set_password(Model_User::hash($_POST['password']));
			}
			$user->save();
			$user->remove_role($user->get_roles());
			if(isset($_POST['roles'])) $user->add_role($_POST['roles']);
			Request::current()->redirect("adm/user/list/");
		} catch (Validation_Exception $e) {
			$this->response->body(Json_View::factory(array("content" => $e->errors())));
		} catch (Exception $e) {
			$errors = array("email" => $e->getMessage());
			$this->response->body(Json_View::factory(array("content" => $errors)));
		}
	}

	public static function format_actions($value) {
		$edit = "<a href='" . url::base() . "adm/user/index/$value'>";
		$edit.= "	<img src='" . url::base() . "modules/dec/static/images/report/edit.png' title='" . __("Edit") . "' alt='" . __('Edit') . "'>";
		$edit.= "</a>";
		$remove = "<a href='#' onclick='ajaxRemove(\"" . url::base() . "adm/user/remove/$value\",\"" . __("Do you really want to remove?") . "\")'>";
		$remove.= "	<img src='" . url::base() . "modules/dec/static/images/report/remove.png' title='" . __('Remove') . "' alt='" . __('Remove') . "'>";
		$remove.= "</a>";
		return $edit . $remove;
	}

} // End User